package com.seu.vcampus.view.library;

import com.seu.vcampus.client.VCampusClient;
import com.seu.vcampus.util.UIStyleUtil;
import com.seu.vcampus.view.UserManagementPanel;
import com.seu.vcampus.vo.User;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * 现代化图书馆主界面
 * 采用按钮跳转模式，整合所有图书馆功能
 * 
 * @author 虚拟校园开发团队
 * @since 2.2.0
 */
public class LibraryMainPanel extends JPanel {
    private VCampusClient client;
    private User currentUser;
    private CardLayout cardLayout;
    private JPanel mainPanel;
    
    public LibraryMainPanel(VCampusClient client, User user) {
        this.client = client;
        this.currentUser = user;
        
        initializeComponents();
        setupLayout();
        setupEventHandlers();
    }
    
    private void initializeComponents() {
        setLayout(new BorderLayout());
        setBackground(UIStyleUtil.BACKGROUND_COLOR);
        setBorder(new EmptyBorder(20, 20, 20, 20));
        
        cardLayout = new CardLayout();
        mainPanel = new JPanel(cardLayout);
        mainPanel.setBackground(UIStyleUtil.BACKGROUND_COLOR);
    }
    
    private void setupLayout() {
        // 创建功能选择面板
        JPanel selectionPanel = createSelectionPanel();
        
        // 创建功能面板
        createFunctionPanels();
        
        // 添加到主面板
        add(selectionPanel, BorderLayout.NORTH);
        add(mainPanel, BorderLayout.CENTER);
    }
    
    private JPanel createSelectionPanel() {
        JPanel panel = UIStyleUtil.createCardPanel("图书馆功能");
        panel.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 15));
        
        // 根据用户类型显示不同功能
        if (currentUser.getUserType() == User.UserType.STUDENT || 
            currentUser.getUserType() == User.UserType.TEACHER) {
            
            panel.add(createFunctionButton("图书查询", "bookSearch"));
            panel.add(createFunctionButton("我的借阅", "myBorrow"));
            panel.add(createFunctionButton("借阅历史", "borrowHistory"));
            
        } else if (currentUser.getUserType() == User.UserType.LIBRARY_ADMIN) {
            
            panel.add(createFunctionButton("图书管理", "bookManagement"));
            panel.add(createFunctionButton("借阅管理", "borrowManagement"));
            panel.add(createFunctionButton("用户管理", "userManagement"));
            panel.add(createFunctionButton("统计分析", "statistics"));
        }
        
        return panel;
    }
    
    private JButton createFunctionButton(String text, String actionCommand) {
        JButton button = UIStyleUtil.createPrimaryButton(text);
        button.setActionCommand(actionCommand);
        button.setPreferredSize(new Dimension(120, 40));
        return button;
    }
    
    private void createFunctionPanels() {
        // 图书查询面板
        BookSearchPanel bookSearchPanel = new BookSearchPanel(client, currentUser);
        mainPanel.add(bookSearchPanel, "bookSearch");
        
        // 我的借阅面板
        MyBorrowPanel myBorrowPanel = new MyBorrowPanel(client, currentUser);
        mainPanel.add(myBorrowPanel, "myBorrow");
        
        // 借阅历史面板
        BorrowHistoryPanel borrowHistoryPanel = new BorrowHistoryPanel(client, currentUser);
        mainPanel.add(borrowHistoryPanel, "borrowHistory");
        
        // 图书管理面板（管理员）
        if (currentUser.getUserType() == User.UserType.LIBRARY_ADMIN) {
            
            ModernLibraryPanel bookManagementPanel = new ModernLibraryPanel(client, currentUser);
            mainPanel.add(bookManagementPanel, "bookManagement");
            
            BorrowManagementPanel borrowManagementPanel = new BorrowManagementPanel(client, currentUser);
            mainPanel.add(borrowManagementPanel, "borrowManagement");
            
            UserManagementPanel userManagementPanel = new UserManagementPanel(client, currentUser);
            mainPanel.add(userManagementPanel, "userManagement");
            
            StatisticsPanel statisticsPanel = new StatisticsPanel(client, currentUser);
        }
        
        // 默认显示图书查询
        cardLayout.show(mainPanel, "bookSearch");
    }
    
    private void setupEventHandlers() {
        // 为选择面板中的按钮添加事件监听
        Component[] components = ((JPanel)getComponent(0)).getComponents();
        for (Component comp : components) {
            if (comp instanceof JButton) {
                JButton button = (JButton) comp;
                button.addActionListener(this::handleFunctionSelection);
            }
        }
    }
    
    private void handleFunctionSelection(ActionEvent e) {
        String command = e.getActionCommand();
        cardLayout.show(mainPanel, command);
        
        // 刷新对应面板的数据
        refreshPanelData(command);
    }
    
    private void refreshPanelData(String panelName) {
        for (Component comp : mainPanel.getComponents()) {
            if (comp instanceof RefreshablePanel) {
                RefreshablePanel panel = (RefreshablePanel) comp;
                panel.refreshData();
            }
        }
    }
    
    /**
     * 可刷新面板接口
     */
    public interface RefreshablePanel {
        void refreshData();
    }
    
    /**
     * 图书查询面板
     */
    private static class BookSearchPanel extends JPanel implements RefreshablePanel {
        private VCampusClient client;
        private User currentUser;
        
        public BookSearchPanel(VCampusClient client, User user) {
            this.client = client;
            this.currentUser = user;
            
            setLayout(new BorderLayout());
            setBackground(UIStyleUtil.BACKGROUND_COLOR);
            
            JLabel label = new JLabel("图书查询功能 - 开发中", SwingConstants.CENTER);
            label.setFont(UIStyleUtil.TITLE_FONT);
            add(label, BorderLayout.CENTER);
        }
        
        @Override
        public void refreshData() {
            // 实现图书查询数据刷新
        }
    }
    
    /**
     * 我的借阅面板
     */
    private static class MyBorrowPanel extends JPanel implements RefreshablePanel {
        private VCampusClient client;
        private User currentUser;
        
        public MyBorrowPanel(VCampusClient client, User user) {
            this.client = client;
            this.currentUser = user;
            
            setLayout(new BorderLayout());
            setBackground(UIStyleUtil.BACKGROUND_COLOR);
            
            JLabel label = new JLabel("我的借阅 - 开发中", SwingConstants.CENTER);
            label.setFont(UIStyleUtil.TITLE_FONT);
            add(label, BorderLayout.CENTER);
        }
        
        @Override
        public void refreshData() {
            // 实现我的借阅数据刷新
        }
    }
    
    /**
     * 借阅历史面板
     */
    private static class BorrowHistoryPanel extends JPanel implements RefreshablePanel {
        private VCampusClient client;
        private User currentUser;
        
        public BorrowHistoryPanel(VCampusClient client, User user) {
            this.client = client;
            this.currentUser = user;
            
            setLayout(new BorderLayout());
            setBackground(UIStyleUtil.BACKGROUND_COLOR);
            
            JLabel label = new JLabel("借阅历史 - 开发中", SwingConstants.CENTER);
            label.setFont(UIStyleUtil.TITLE_FONT);
            add(label, BorderLayout.CENTER);
        }
        
        @Override
        public void refreshData() {
            // 实现借阅历史数据刷新
        }
    }
}